package com.apes.scm.masterdata.product.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.product.model.ProductUomCategory;
import com.apes.scm.masterdata.product.repository.ProductUomCategoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * 商品计量单位类别
 *
 * @author wuq
 * @create 2018-08-02 15:22
 */
@Service("productUomCategoryService")
public class ProductUomCategoryService extends DomainService{

    @Autowired
    ProductUomCategoryRepository uomCategoryRepository;

    public ProductUomCategory save(SimpleRequest request) {
        ProductUomCategory productUomCategory = request.getO(ProductUomCategory.class);
        return uomCategoryRepository.saveAndFlush(productUomCategory);
    }

    public String delete(SimpleRequest request){
        String id = request.get("id");

        ProductUomCategory productUomCategory = uomCategoryRepository.findOne(id);
        if (productUomCategory == null) throw new RuntimeException("商品编码：" + request.get("id") + " 在系统中不存在。");

        uomCategoryRepository.delete(productUomCategory);
        return "删除成功！";
    }

    public ProductUomCategory update(SimpleRequest request) {
        ProductUomCategory productUomCategory = request.getO(ProductUomCategory.class);
        return uomCategoryRepository.saveAndFlush(productUomCategory);
    }


    /**
     * 功能：下拉模糊查询使用
     * @param request
     * @return
     */
    public Page<ProductUomCategory> find(SimpleRequest request) {
        String value = request.get("key");
        return uomCategoryRepository.find(value, value, PageRequest.of(request.get("page"), request.get("size")) );
    }

}
